package com.sorted.utils;

import com.sorted.ad.SortMaxReward;
import com.sorted.bridge.SortCocosBridge;

import org.json.JSONObject;

public class SortADManager {

    private static SortADManager sortADManager;

    public static SortADManager getInstance() {
        if (sortADManager == null) {
            sortADManager = new SortADManager();
        }
        return sortADManager;
    }

    public void playAd(String adSource) {
        try {
            if (!SortMaxReward.getInstance().isReady()) {
                callbackAdError();
                return;
            }
            callback(adSource);
        } catch (Exception e) {
            e.printStackTrace();
            callbackAdError();
        }
    }

    private void callback( String adSource) {
        SortMaxReward.getInstance().showAd(SortCocosBridge.mainGameActivity.get(), adSource);

    }

    public void callbackAdOver(String adSource) {
        try {
            JSONObject jsonObject = getJson(adSource);
            SortCocosBridge.getInstance().XSSdkCallback("ad_over_wha", jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void callbackAdError() {
        try {
            SortCocosBridge.getInstance().XSSdkCallback("ad_error_wha", "{}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source", adSource);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return jsonObject;
    }

}
